﻿using AutoMapper;
using SIG.DAL.Dapper.Model;
using SIG.DAL.Dapper.Repositories;
using SIG.Service;
using SIG.ViewModel.Front.Links;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace SIG.WebMVC.Controllers
{
    public class LinkController : BaseController
    {
        private readonly LinkCategoryRepository _categoryService;
        private readonly LinkRepository _linkService;
        private readonly PageMetaRepository _pageMetaService;
        private readonly IMapper _mapper;

        public LinkController(IMapper mapper)
        {
            _categoryService = new LinkCategoryRepository();
            _linkService = new LinkRepository();
            _pageMetaService = new PageMetaRepository();
            _mapper = mapper;

        }

        public ActionResult Index(int? categoryId)
        {
            IEnumerable<Link> lLinks = _linkService.GetActiveItems().OrderByDescending(m=>m.Importance);

            if (categoryId > 0)
                lLinks = lLinks.Where(l => l.CategoryId == categoryId);

            PageLinkVM vm = new PageLinkVM();
            vm.Categories = _categoryService.GetActiveItems().OrderByDescending(m => m.Importance);
            //  = _mapper.Map<List<LinkCategory>, List<LinkCategoryVM>>(categories);
            vm.Links = lLinks;

            var url = Request.RawUrl;
            vm.PageMeta = _pageMetaService.GetPageMeta(ModelType.MENU, url);

            return View(vm);

        }
         public PartialViewResult LatesterLinks()
        {
            var links = _linkService.GetActiveItems().OrderByDescending(m => m.Importance).Take(8);
            return PartialView("_LatesterLinks", links);
        }
    }
}